package com.ag.utils.vo;

import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.ag.utils.DateUtils;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@NoArgsConstructor
@SuperBuilder
@EqualsAndHashCode(callSuper = false)
public class TimeQuery {
	protected String dateStart;
	protected String dateEnd;
	
	protected String dayStart;
	protected String dayEnd;
	
	protected TimeFlag timeFlag;
	
	public void timeCoverage(TimeQuery query) {
		this.dateStart = query.dateStart;
		this.dateEnd = query.dateEnd;
		this.dayStart = query.dayStart;
		this.dayEnd = query.dayEnd;
		this.timeFlag = query.timeFlag;
	}
	
	public void check() {
		if (timeFlag != null && timeFlag.start() != null && timeFlag != null) {
			dateStart = DateUtils.format(timeFlag.start());
			dateEnd = DateUtils.format(timeFlag.end());
		}
		
		if(StringUtils.isNotBlank(dayStart)) {
			dateStart = dayStart + " 00:00:00";
		}
		if(StringUtils.isNotBlank(dayEnd)) {
			dateEnd = dayEnd + " 23:59:59";
		}
		
	}
	
	public Date dateStart() {
		return DateUtils.parse(dateStart);
	}
	
	public Date dateEnd() {
		return DateUtils.parse(dateEnd);
	}
	
	public enum TimeFlag {
		all {
			@Override
			public Date start() {
				return null;
			}
			
			@Override
			public Date end() {
				return null;
			}
		},
		today {
			@Override
			public Date start() {
				return DateUtils.getTodayStartDate();
			}
			
			@Override
			public Date end() {
				return DateUtils.getTodayEndDate();
			}
		},
		yesterday {
			@Override
			public Date start() {
				return DateUtils.getDayStartDate(DateUtils.dayAdd(-1));
			}
			
			@Override
			public Date end() {
				return DateUtils.getDayEndDate(DateUtils.dayAdd(-1));
			}
		},
		nearly3Days {
			@Override
			public Date start() {
				return DateUtils.getDayStartDate(DateUtils.dayAdd(-2));
			}
			
			@Override
			public Date end() {
				return DateUtils.getTodayEndDate();
			}
		},
		nearly7Days {
			@Override
			public Date start() {
				return DateUtils.getDayStartDate(DateUtils.dayAdd(-6));
			}
			
			@Override
			public Date end() {
				return DateUtils.getTodayEndDate();
			}
		},
		nearly30Days {
			@Override
			public Date start() {
				return DateUtils.getDayStartDate(DateUtils.dayAdd(-29));
			}
			
			@Override
			public Date end() {
				return DateUtils.getTodayEndDate();
			}
		};
		
		public Date start() {
			throw new UnsupportedOperationException();
		}
		
		public Date end() {
			throw new UnsupportedOperationException();
		}
	}
}
